package com.blog.service.impl;

import com.blog.mapper.ArticleMapper;
import com.blog.mapper.StatisticsMapper;
import com.blog.model.ArticleInfo;
import com.blog.model.ArticleUser;
import com.blog.model.DiscussInfo;
import com.blog.model.StatisticalModel;
import com.blog.pojo.Article;
import com.blog.pojo.Discuss;
import com.blog.pojo.Statistics;
import com.blog.pojo.User;
import com.blog.service.ArticleService;
import com.blog.service.DiscussService;
import com.blog.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author JiaFuJun
 * @version v1.0
 * @date 2020-05-13
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private DiscussService discussService;
    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public void save(Article record) {
        if (record.getId() == null) {
            record.setCreateTime(DateUtil.getStrTime());
            articleMapper.insert(record);
        } else {
            articleMapper.update(record);

        }
    }

    @Override
    public void update(Article record) {
        articleMapper.update(record);
    }

    @Override
    public void del(Object id) {
        discussService.delByArtId(id);
        articleMapper.del(id);
    }

    @Override
    public Article selectByPrimaryKey(Object id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Article> getList(Article record) {
        return articleMapper.selectByRecord(record);
    }

    @Override
    public PageInfo<Article> pageList(Article record, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Article> list = articleMapper.selectByRecord(record);
        return new PageInfo<>(list);
    }

    @Override
    public ArticleInfo selectById(Integer id) {
        ArticleUser info = articleMapper.selectById(id);
        List<ArticleUser> recommend = recommend();
        List<DiscussInfo> discusses = discussService.getByArticleId(info.getAId());
        return new ArticleInfo(info, recommend, discusses);
    }

    @Override
    public Map<String, Object> getData() {
        Map<String, Object> data = new HashMap<>();
        ArticleUser first = articleMapper.getFirst();
        data.put("first", first);
        List<ArticleUser> list = articleMapper.getData(first.getAId());
        data.put("list", list);
        return data;
    }

    @Override
    public List<ArticleUser> recommend() {
        return articleMapper.recommend();
    }

    @Override
    public void delByUserId(Integer id) {
        Article article = new Article();
        article.setUserId(id);
        List<Article> list = articleMapper.selectByRecord(article);
        articleMapper.delByUserId(id);
        for (Article art : list) {
            discussService.delByArtId(art.getId());
        }
    }

    @Override
    public StatisticalModel statisticalData(Integer id) {
        StatisticalModel model = new StatisticalModel();
        Map<String, Object> param = new HashMap<>();
        String endTime = DateUtil.getStrDate();
        String startTime = DateUtil.getStrDate(DateUtil.getDateToDateNum(new Date(), -30));
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("userId", id);
        Integer publishNum = articleMapper.getPublishNum(param);
        Integer publishNumTotal = articleMapper.getPublishNumTotal(param);
        model.setPublishNum(publishNum);
        model.setPublishNumTotal(publishNumTotal);
        Integer browseNum = articleMapper.getBrowseNum(param);
        Integer browseNumTotal = articleMapper.getBrowseNumTotal(param);
        model.setBrowseNum(browseNum);
        model.setBrowseNumTotal(browseNumTotal);
        Integer praiseNum = articleMapper.getPraiseNum(param);
        Integer praiseNumTotal = articleMapper.getPraiseNumTotal(param);
        model.setPraiseNum(praiseNum);
        model.setPraiseNumTotal(praiseNumTotal);
        Integer discussNum = articleMapper.getDiscussNum(param);
        Integer discussNumTotal = articleMapper.getDiscussNumTotal(param);
        model.setDiscussNum(discussNum);
        model.setDiscussNumTotal(discussNumTotal);
        List<Statistics> list = statisticsMapper.selectSatistics(id, startTime,endTime);
        List<Date> dateList = DateUtil.getDatesBetweenDate( DateUtil.getDateForStr(startTime),DateUtil.getDateForStr(endTime));
        List<Map<String, Object>> dataList = new ArrayList<>();
        dateList.forEach(date -> {
            Map<String, Object> data = new HashMap<>();
            String dateTime = DateUtil.getStrDate(date);
            data.put("date", dateTime);
            data.put("value", 0);
            list.forEach(statistics -> {
                if (statistics.getDataTime().equals(dateTime)) {
                    data.put("value", statistics.getBrowseNum());
                }
            });
            dataList.add(data);
        });
        model.setBrowseList(dataList);
        return model;
    }

    @Override
    public void browseRecord(Article article) {
        update(article);
        Statistics statistics = new Statistics();
        statistics.setArticleId(article.getId());
        statistics.setDataTime(DateUtil.getStrDate());
        statistics.setBrowseNum(1);
        Statistics info = statisticsMapper.selectByTime(statistics);
        if (info == null) {
            statisticsMapper.insert(statistics);
        } else {
            statisticsMapper.update(statistics);
        }
    }

}